package com.wcity.manage.web;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DigestUitl;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.model.Act;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.ActService;
import com.wcity.manage.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录控制层
 *
 * @author dy
 */
@Controller
public class LoginAct {

    /**
     * 欢迎页面
     *
     * @return
     */
    @RequestMapping( "/" )
    public String index() {
        return "login/login";
    }

    /**
     * 转到主页
     *
     * @return
     */
    @RequestMapping( "index" )
    public String goIndex( HttpServletRequest request ) {
        request.setAttribute( "FINANCE_ROLE_ID", 5 );
        request.setAttribute( "MANAGER_ROLE_ID", 4 );
        request.setAttribute( "ADMIN_ROLE_ID", 3 );
        request.setAttribute( "SUPER_ADMIN_ROLE_ID", 2 );
        request.setAttribute( "BUILDER_ROLE_ID", 1 );
        return "index";
    }

    /**
     * 转到登录页面
     *
     * @return
     */
    @RequestMapping( "to_login" )
    public String goLogin() {
        return "login/login";
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "login" )
    public String login( String username, String password,
                               HttpServletRequest request, HttpServletResponse response ) {

        User user = userService.getUser( username );
        String message;
        if ( username == null ) {
            message = null;
        } else if ( user == null || !user.getIsAvailable() ) {
            message = "用户不存在";
        } else if ( !user.getPwd().equals( DigestUitl.pwd( password ) ) ) {
            message = "用户名或密码错误";
        } else {
            loginSuccess(request, user);
            return RespUtil.redirect( response, "index" );
        }
        request.setAttribute( "msg", message );
        request.setAttribute("username",username);
        return "login/login";
    }

    /**
     * 转到Dialog登录页面
     *
     * @return
     */
    @RequestMapping( "to_login_dialog" )
    public String goLoginDialog() {
        return "login/logindialog";
    }

    /**
     * Dialog登录
     *
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "login_dialog" )
    public void loginDialog( String username, String password,
                               HttpServletRequest request, HttpServletResponse response ) {

        User user = userService.getUser( username );
        String message = "";
        if ( user == null || !user.getIsAvailable() ) {
            message = "用户不存在";
        } else if ( !user.getPwd().equals( DigestUitl.pwd( password ) ) ) {
            message = "用户名或密码错误";
        } else {
            loginSuccess( request, user );
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "", "", "", "closeCurrent", "" ) );
            return;
        }
        RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", message, "", "", "", "" ) );
    }

    /**
     * 登陆成功
     *
     * @param request
     * @param user
     */
    private void loginSuccess( HttpServletRequest request, User user ) {

        userService.updateLastLoginTime( user.getId() );
        request.getSession().setAttribute( SessionKey.SESSION_USER_INFO, user );
        // 设置左侧导航栏
        List<Act> actList = actService.getNavActList( user );
        String navTree = DwzUtil.getNavStr( actList, new StringBuffer() );
        request.getSession().setAttribute( SessionKey.SESSION_NAV_STR, navTree );
    }

    /**
     * 登出
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "logout" )
    public String logout(  HttpServletRequest request, HttpServletResponse response ) {
        request.getSession().removeAttribute( SessionKey.SESSION_USER_INFO );
        request.getSession().removeAttribute( SessionKey.SESSION_NAV_STR );
        return RespUtil.redirect( response, "to_login" );
    }

    @Resource private UserService userService;
    @Resource private ActService actService;

}
